#include <unistd.h>
#include <stdlib.h>

#include "ChatClient.hpp"
#include "ChatWindow.hpp"

void menu()
{
    cout << "***********************************" << endl;
    cout << "*****1.Register*******2.Login******" << endl;
    cout << "*****3.Loginout*******4.Exit ******" << endl;
    cout << "***********************************" << endl;
}

int main(int argc,char* argv[])
{
    //./ChatClient -ip [ip]
    if(argc != 3)
    {
        cout << "using ./ChatClient -ip [ip]" << endl;
        return -1;
    }
    string ip;
    for(int i = 0;i < argc;i++)
    {
        if(strcmp(argv[i],"-ip") == 0 && (i+1)<argc)
        {
            ip = argv[i+1];
        }
    }
    if(ip.size() == 0)
    {
        cout << "please restart client" << endl;
        return -2;
    }
        ChatClient* cc = new ChatClient(ip);
        ChatWindow* cw = new ChatWindow();

    while(1)
    {
        menu();
        int select = -1;
        cout << "please enter your selection : " <<endl;
        fflush(stdout);
        cin >> select;
        switch(select)
        {
            //注册
            case 1:
                {
                    int ret = cc->RegisterInfo();
                    if(ret < 0)
                    {
                        LOG(ERROR,"cc->RegisterInfo faild") << endl;
                    }
                    else if(ret == 0)
                    {
                        LOG(ERROR,"register success! please login..") << endl;
                    }
                    cc->CloseFd();
                    break;
                }
            case 2:
                {
                    int ret = cc->LoginInfo();
                    if(ret < 0)
                    {
                        LOG(ERROR,"cc->LoginInfo faild") << endl;
                    }
                    else if(ret == 0)
                    {
                        LOG(INFO,"login success,please chatting..") << endl;
                        cw->Start(cc);
                    }   
                    break;
                }
            case 3:
                {
                    break;
                }
            case 4:
                {
                    _exit(0);
                    break;
                }
        }
    }
    
    while(1)
    {
        sleep(1);
    }
    return 0;
}
